# -*- coding:utf-8 -*-
import tkinter as tk
import tkinter.colorchooser
import tkinter.ttk

from dpi_fitting import dpi_fitting

class MainAPP(object):
    def __init__(self):
        self.main = tk.Tk()

        dpi_fitting(self.main)
        
        self.main.title('颜色选择器')
        self.main.bind('<Return>',self.choose)
        self.createwidget()

    def createwidget(self):
        self.n1 = tk.StringVar()
        self.n2 = tk.StringVar()
        self.showcolor = tk.Label(self.main,width = 60,height = 15)
        
        self.nameframe1 = tk.Frame(self.main)
        tk.Label(self.nameframe1,text = '您选择的颜色是：').pack(side = 'left')
        self.l1 = tk.Label(self.nameframe1,textvariable = self.n1,font = ('黑体',15))
        self.l1.pack(side = 'right')
                                                              
        self.nameframe2 = tk.Frame(self.main)
        tk.Label(self.nameframe2,text = 'RGB颜色值约为：').pack(side = 'left')
        self.l2 = tk.Label(self.nameframe2,textvariable = self.n2)
        self.l2.pack(side = 'right')
                                                         
        self.choosebutton = tk.ttk.Button(self.main,text = '选择',command = self.choose)
                                                         
        self.showcolor.pack()
        self.nameframe1.pack()
        self.nameframe2.pack()
        self.choosebutton.pack()

    def choose(self,event = None):
        color = self.n1.get() if self.n1.get() else '#ff9ff3'
        color = tk.colorchooser.askcolor(color = color)
        if color[1] is None:return
        rgb = [int(one) for one in color[0]]
        self.n1.set(color[1])
        self.n2.set(rgb)
        self.showcolor.configure(bg = color[1])
        bg = '#7f7f7f' if sum(rgb) >= 600 else '#e9e9e9'
        self.l1.configure(fg = color[1],bg = bg)


    def start(self):
        self.main.mainloop()


if __name__ == '__main__':
    MainAPP().start()
